package com.rr.utils.core;


import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

public class TableSupport
{
    /**
     * 当前记录起始索引
     */
    public static final String PAGE_NUM = "pageNum";

    /**
     * 每页显示记录数
     */
    public static final String PAGE_SIZE = "pageSize";

    /**
     * 排序列
     */
    public static final String ORDER_BY_COLUMN = "orderByColumn";

    /**
     * 排序的方向 "desc" 或者 "asc".
     */
    public static final String IS_ASC = "isAsc";

    /**
     * 分页参数合理化
     */
    public static final String REASONABLE = "reasonable";

    /**
     * 封装分页对象
     */
    public static PageDomain getPageDomain()
    {
        /**
         * 获取当前请求
         */
        ServletRequestAttributes attributes = (ServletRequestAttributes)RequestContextHolder
                .getRequestAttributes();
        HttpServletRequest  request = attributes.getRequest();
        /**
         * 取出参数 new 分页请求参数对象
         */

        return new PageDomain
                .Builder()
                .pageNum( Integer.parseInt(request.getParameter(PAGE_NUM)))
                .pageSize( Integer.parseInt(request.getParameter(PAGE_SIZE)))
                .orderByColumn(request.getParameter(ORDER_BY_COLUMN))
                .build();

    }

    public static PageDomain buildPageRequest()
    {
        return getPageDomain();
    }
}